/********************/
/* Recensement 1999 */
/********************/
libname rp99 'S:\RP\RP 1999\Exploitation compl�mentaire\Version 1';
option ls=80 mprint;
libname mini "P:\Donnees\RPMini";
libname ccom "P:\Donnees\Code_commune";

proc contents data=rp99.tabind991 ;run;
/* charge la table RP 99 */
data sample;
set 
rp99.tabind991 
( keep = d c uu99 tu99 sond )
 rp99.tabind992 
( keep = d c uu99 tu99 sond )
 rp99.tabind993 
( keep = d c uu99 tu99 sond )
 rp99.tabind995 
( keep = d c uu99 tu99 sond )
 rp99.tabind9941
( keep = d c uu99 tu99 sond )
 rp99.tabind9942
( keep = d c uu99 tu99 sond )
;
run;
/*
data sample;
set mini.rp99mini;
run;
*/
/* garde seulement les unit�s urbaines et le code commune */
data sample;
length depcom $5;
set sample (keep = d c uu99 tu99 sond);
depcom = cats(d,c);
drop d c;
run;

/* harmonise en prenant le code commune de 2012 */
data cg12;
set ccom.cg12;
run;
proc sort data=cg12 nodupkeys;
by depcom;
run;

proc sort data=sample;
by depcom;
run;

data sample;
merge sample (in = x) cg12;
by depcom;
if x;
run;

/* trie par commune */
proc sort data=sample nodupkeys out=sample2;
by depcom;
run;

data ccom.uu99;
set sample2;
drop depcom sond;
run;
/* Elimine les doublons */
proc sort data=ccom.uu99 nodupkeys;
by cg12;
run;

/* Names : cr�e une table avec le nom de la commune plus peupl�e pour chaque UU */
data names;
length cg12 $5;
set ccom.France2012 (keep =dep com ncc);
cg12=cats(dep,com);
drop dep com;
run;

/* find biggest cities in each uu */
proc sort data=sample;
by uu99;
run;
proc freq data=sample noprint;
by uu99;
weight sond;
table cg12 / out = popuu99;
run;
proc sort data=popuu99;
by uu99 count;
run;
data popuu99big;
set popuu99;
by uu99;
if not(last.uu99) then delete;
run;
proc sort data=popuu99big;
by cg12;
run;
proc sort data=names;
by cg12;
run;
data popuu99big;
merge popuu99big (in = x) names;
by cg12; if x;
run;
data popuu99big;
set popuu99big (keep = uu99 ncc);
rename ncc= nameUU;
run;
proc sort data=popuu99big nodupkeys;
by uu99; run;
proc sort data=ccom.uu99;
by uu99; run;
data ccom.uu99;
merge ccom.uu99 (in = x) popuu99big;
by uu99;
if x;
run;
proc sort data=ccom.uu99;
by cg12; 
run;
data namescg12;
set names;
rename ncc = nameCC ;
run;
proc sort data=namescg12 nodupkeys;
by cg12;
run;
proc sort data=ccom.uu99;
by cg12;
run;
data ccom.uu99;
merge ccom.uu99 (in = x) namescg12 ;
by cg12;
if x;
run;
data ccom.uu99_names;
set ccom.uu99;
run;
data ccom.uu99;
set ccom.uu99;
drop nameUU nameCC;
run;

proc export data=ccom.uu99 outfile="P:\Donnees\Code_commune\uu99.dta" replace;
run;
proc export data=ccom.uu99 outfile="P:\Donnees\Code_commune\uu99_names.dta" replace;
run;
data uu99_names2;
set ccom.uu99_names (keep = uu99 tu99 nameuu);
run;
proc sort data=uu99_names2 nodupkeys;
by uu99;
run;
proc export data=uu99_names2 outfile="P:\Donnees\Code_commune\uu99_nmSHORT.dta" replace;
run;
